package com.itheima.ydd.controller;

import com.itheima.ydd.commom.R;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.service.YddArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 分页查询
     *
     * @param startTime
     * @param endTime
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    //required = false  表示不传值也不会报错
    public R list(@RequestParam(value = "start_time", required = false) Long startTime,
                  @RequestParam(value = "end_time", required = false) Long endTime,
                  @RequestParam(value = "title", required = false) String title,
                  Integer cateid,
                  int page, int limit
    ) {
        return yddArticleService.list(startTime, endTime, title, cateid, page, limit);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R rowInfo(Integer id) {
        log.info("传入的id是：{}", id);
        YddArticle byId = yddArticleService.getById(id);
        return R.success("", byId);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R del(Integer id) {
        YddArticle byId = yddArticleService.getById(id);
        yddArticleService.removeById(byId);
        return R.success("", null);
    }

    /**
     * 新增文章
     *
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody YddArticle yddArticle) {
        yddArticleService.save(yddArticle);
        return R.success("", yddArticle);
    }

    /**
     * 修改文章
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddArticle yddArticle) {
        yddArticleService.updateById(yddArticle);
        return R.success("", yddArticle);
    }

}
